package com.eliza.music.model.db

import android.content.Context
import androidx.room.Room
import com.eliza.music.model.constant.Music_DATABASE_NAME
import com.eliza.music.model.dao.SongDao
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
object MusicDatabaseModule {
    @Provides // @Provides 注解，告知 Hilt 如何提供无法通过构造函数注入的类型
    @Singleton //单例模式
    fun musicDatabase(
        @ApplicationContext context: Context,
    ): MusicDatabase = Room.databaseBuilder(
        context,
        MusicDatabase::class.java,
        Music_DATABASE_NAME
    )
        // .allowMainThreadQueries() //绕过主线程不可操作数据库
        .fallbackToDestructiveMigration()
        .build()

    // @Provides 注解，告知 Hilt 如何提供无法通过构造函数注入的类型
    @Provides
    @Singleton
    fun songDao(
        database: MusicDatabase,
    ): SongDao = database.songDao()
}
